package com.twu.biblioteca.users;

import java.util.HashMap;
import java.util.Map;

public class AuthenticateUser {

    private Map<String, User> users;
    private String userName;
    private String password;

    public AuthenticateUser() {
        users = new HashMap<String, User>();
        users.put("111-1111", new User("James", "james@email.com", "+919940202502", "111-1111", "password"));
        users.put("111-1112", new User("Tim", "tim@email.com", "+919978202502", "111-1112", "password1"));
        users.put("111-1113", new User("Peter", "peter@email.com", "+9199445302502", "111-1113", "password1"));
    }

    protected User findUser(String libraryNumber) {
        return users.get(libraryNumber);
    }

    public void promptUserForCredentials() {
        System.out.println("Enter your library number and password seperated by a space: ");
    }

    public void captureUserNameAndPassword(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public User validateUser() {
        User user = findUser(this.userName);
        try {
            if (user.authenticate(this.password)){
                System.out.println("Found the user");
                return user;
            }
        } catch (NullPointerException e) {
            throw e;
        }
        return null;
    }
}
